/**
 * Created by Administrator on 2016/9/5.
 */

function isInteger(obj) {
	return obj % 1 == 0;
}

/**
 * 题型公共属性及方法
 * @constructor
 */
var TopicCommonModule = function(typeId, pCategory) {
	//题型（同样也是题型Id）
	this.typeId = typeId;
	//题型名称
	this.topicName = "";
	//默认开始题号
	this.topicStartIndex = 1;
	//默认选项个数
	this.topicSelectCount = 4;
	//默认结束题号
	this.topicEndIndex = 0;
	//单选题题目最大个数
	this.singleMaxCount = 150;
	//选项最大个数
	this.selectMaxCount = 12;
	//选项最小个数
	this.selectMinCount = 2;
	//栏数最大个数
	this.subfieldMaxCount = 5;
	//英语作文栏数最大个数
	this.EnglishCompositionMaxCount = 20;
	//作文题最大格子数
	this.compositionMaxCount = 2000;
	//题目最大数
	this.topicMaxCount = 150;
	//填空题数量限制
	this.fillBlankMaxCount = 48;
	//当前最大题号
	this.maxTopicIndex = 1;
	//创建题目数
	this.createTopicCount = 0;
	//指定题型Id
	this.topicTypeDescId = "";
	//是否创建新题型
	this.isCreateTopicType = true;
	//在客观题中添加的是单选题还是多选题由用户指定
	this.addTypeId = this.typeId;
	//题型父类型(如：英语作文题)
	if(pCategory) { //所属题型
		this.pCategory = pCategory;
	}
};

/**
 * 题型初始化
 */
TopicCommonModule.prototype.initTopicCommonModule = function() {
	this.initDataView();
	this.baseBindEvent();
};

/**
 * 题型相关事件绑定
 */
TopicCommonModule.prototype.baseBindEvent = function() {
	var _this = this;
	this.offsetEvent();

	var tmp1 = _this.typeId;
	var tmp2 = _this.pCategory ? _this.pCategory : _this.typeId;
	if(tmp1 == "Composition" || tmp1 == "EnglishComposition") {
		var nameStr = tmp2 + "_"; //+_this.addSmallTopicType+"_"
	} else {
		var nameStr = tmp2 + "_" + tmp1; //+_this.addSmallTopicType+"_"
	}

	//topicStartIndex
	//titleScoreNumRow_Object_Object_4
	//titleScoreNumRow_MultiChoice_MultiChoice_4
	//titleScoreNumRow_SingleChoice_SingleChoice_4_选择题
	//titleScoreNumRow_AnswerQuestion_FillBlank_8_填空题_9
	//titleScoreNumRow_AnswerQuestion_AnswerQuestion_3_解答题_3
	//titleScoreNumRow_Composition__1_语文作文题_33
	//titleScoreNumRow_AnswerQuestion_EnglishComposition_10_英语作文题_12

	var qstr = "tr[id^='titleScoreNumRow_" + nameStr + "']"; //this.topicStartIndex+
	$(document).delegate(qstr, 'click', function(e) {
		_this.clearMsg();
		if(DescCtrl.getAnswerSheetModel()) {
			_this.listPopShow(tmp1, tmp2, e.currentTarget.id, DescCtrl.answerSheetDesc.isTranscriptsPoints);
		}
	});

	$("#btn_" + _this.typeId).on("click", function() {
		_this.isCreateTopicType = true;
		_this.clearMsg();
		_this.popShow(DescCtrl.answerSheetDesc.isTranscriptsPoints);
	});

	$("#btn_" + _this.typeId + "_submit").on("click", function() {
		//if (_this.typeId == "AnswerQuestion" || _this.typeId == "Composition" || _this.typeId == "EnglishComposition" || _this.typeId == "FillBlank") {

		//    var scoBox = $('input[name=' + _this.typeId + '_type]');

		//    //是否显示 打分框 
		//    if (!scoBox.get(0).checked) {
		//        var score = $.trim($("#txt_" + _this.typeId + "_score").val());
		//        if (score == "") {
		//            return window.Util.Dialogue.dialogBox(true, "温馨提示", "请输入满分值!");
		//        }
		//    }
		//}
		sheetsFrame.window.CreateSheet.saveEditorData();
		_this.checkTopicCondition();

	});

	$("#btn_" + _this.typeId + "_cancel").on("click", function() {
		_this.popHide();
		_this.clearPopData();
	});

	$("#btn_" + _this.typeId + "_close").on("click", function() {
		_this.popHide();
		_this.clearPopData();
	});

	$("#btn_" + _this.typeId + "_delete").on("click", function() {
		var count = parseInt($("#td_" + _this.typeId + "_count").html());
		if(count > 0) {
			window.Util.Dialogue.confirmBox(true, "确认框", errorMessage.TopicListDelete, function() {
				_this.deleteTopicByTopicType();
			});
		}
	});

	//遍历大题，如果有题目隐层题干，题干显示选项置为否
	_this.resetShowQuestionDesc();
};

/**
 * 题号列表中，点击每行弹出选项
 * @param type 每行的题目类型
 */
TopicCommonModule.prototype.listPopShow = function(type, pType, trid, isTranscriptPoint) {
	var typeStr = type;
	//if (type == "FillBlank") {
	//    //非自制答题卡 英语科目支持设置是否带框
	//    if (from != "manual" && subjectCode == "03") {
	//        $("tr[id=FillBlank_Type]").show();
	//        $("input[name=fillBlank_type]").attr("disabled", false);
	//    } else {
	//        if (isTranscriptPoint) {
	//            //誊分模式为带框
	//            $("input[id=rdo_FillBlank_frame]").prop("checked", true);
	//        } else {
	//            $("input[id=rdo_FillBlank_pure]").prop("checked", true);
	//        }
	//        $("tr[id=FillBlank_Type]").hide();
	//    }
	//    $("input[id=txt_FillBlank_select_count]").attr("disabled", false);

	//}
	if(type == "SingleChoice" || type == "MultiChoice" || type == "Object") {
		//禁止切换题目类型和选项个数
		//ChoiceTypeRow_SingleChoice
		$("input[id=txt_" + typeStr + "_select_count]").attr("disabled", true);
		$("input[name=objectType_" + typeStr + "]").attr("disabled", true);
	}
	//显示题干选项
	if(from != "manual") {
		$(".answerPanelRow").show();
	}

	this.setPopData3(type, pType, trid, isTranscriptPoint);
	$("a[name='btn_topics']").removeAttr("class");
	$("#btn_" + typeStr).attr("class", "on");
	$("#pop_" + typeStr).show(); //+"_"+type
	$("#pop_" + typeStr).attr("dataTag", "on");
	$("#showMask").show();
	document.onkeydown = function(event) {
		if(event.keyCode == 13) {
			if($("#pop_" + typeStr + ":visible").length > 0) {
				$("#btn_" + typeStr + "_submit").trigger("click");
			}
		}
	};
};

/**
 * 根据点击的题目列表行的信息设置弹出框的表单参数
 * @param type 每行的题目类型
 * @param pType 每行的题目父类型
 * @param trid 当前点击节点的id
 */
TopicCommonModule.prototype.setPopData3 = function(type, pType, trid, isTranscriptPoint) {
	var typeStr = type;
	var startNum = trid.split("_")[3]; //titleScoreNumRow_Object_MultiChoice_8_选择题（多）_4
	if(type == "SingleChoice" || type == "MultiChoice" || type == "Object") {
		//获取选项个数：列表中没有客观题！
		var topicype = DescCtrl.getTopicTypeDescs(type).concat(DescCtrl.getTopicTypeDescs("Object"));
		var optionCount;
		topicype.forEach(function(topType) {
			for(var i = 0; i < topType.topicDescs.length; i++) {
				if(topType.topicDescs[i].no == startNum || topType.topicDescs[i].noList[0] == startNum) {
					optionCount = topType.topicDescs[i].optionCount;
				}
			}
		});
		$("#ck_showQuestionTitle_" + type).prop("checked", topicype[0].showQuestionStem);
		if(!optionCount) {
			optionCount = 4
		}
		$("#txt_" + typeStr + "_select_count").val(optionCount);
		//禁止修改选项个数
	} else {
		var topicTypes = DescCtrl.getTopicTypeDescs("AnswerQuestion", type).concat(DescCtrl.getTopicTypeDescs(type));
		//显示题干反写
		$("#ck_showQuestionTitle_" + typeStr).prop("checked", topicTypes[0].showQuestionStem);
	}

	if(type == "Composition" || type == "EnglishComposition") {
		//点击题目列表不能更改作文横线数或格子数
		$("#txt_" + typeStr + "_select_count").attr("disabled", true);
	}
	if(type == "FillBlank") {
		//反写每行题数
		var topicTypes = DescCtrl.getTopicTypeDescs("AnswerQuestion", type);
		topicTypes.forEach(function(topType) {
			if(topType.topicDescs[0].no == startNum || topType.topicDescs[0].noList[0] == startNum) {
				if(topType.topicDescs[0].showQuestionDesc) {
					$("#txt_" + typeStr + "_select_count").attr("disabled", true);
				} else {
					$("#txt_" + typeStr + "_select_count").attr("disabled", false);
				}
				$("#txt_" + typeStr + "_select_count").val(topType.colCount);

				//if (topType.topicDescs[0].smallTopicType == "PureBlank") {
				//    $("#rdo_FillBlank_pure").prop("checked", true);
				//} else {
				//    $("#rdo_FillBlank_frame").prop("checked", true);
				//}

				//if (DescCtrl.getAnswerSheetModel() && subjectCode == "03") {
				//    var id = $("input:radio[name='fillBlank_type']:checked").attr("id");
				//    if (id == "rdo_FillBlank_pure") {
				//        $(".answerPanelRow").hide();
				//        $("#txt_" + typeStr + "_select_count").attr("disabled", true);
				//    } else {
				//        $(".answerPanelRow").show();

				//    }
				//}
			}

			/*topType.topicDescs.forEach(function(topic){
			 if(topic.no== startNum || topic.noList[0] == startNum){
			 if(topType.showQuestionStem){
			 $("#txt_" + typeStr + "_select_count").attr("disabled",true);
			 }else {
			 $("#txt_" + typeStr + "_select_count").attr("disabled",false);
			 }
			 $("#txt_" + typeStr + "_select_count").val(topType.colCount);
			 }
			 if(topic.smallTopicType == "PureBlank"){
			 $("rdo_FillBlank_pure").prop("checked",true);
			 }else {
			 $("rdo_FillBlank_frame").prop("checked",true);
			 }

			 });*/
		});
	}
	$("#txt_" + typeStr + "_name").val($("tr[id='" + trid + "'] #" + type + "_name").html());

	var str = $("tr[id='" + trid + "'] #td_" + type + "_count").html();
	if(str && (str.indexOf(",<br>") != -1 || str.indexOf("~") != -1)) {
		var min = Math.min.apply(null, str.replace(/,<br>/g, "~").replace(/<br>/g, "").split("~"));
		var second = parseFloat(str.replace(/,<br>/g, "~").replace(/<br>/g, "").split("~")[1]);
		var max = Math.max.apply(null, str.replace(/,<br>/g, "~").replace(/<br>/g, "").split("~"));
		$("#txt_" + typeStr + "_start_index").val(min);
		$("#txt_" + typeStr + "_end_index").val(max);

		$("#txt_" + typeStr + "_start_index").attr("data", min);
		$("#txt_" + typeStr + "_end_index").attr("data", max);

	} else {
		$("#txt_" + typeStr + "_start_index").val(str);
		$("#txt_" + typeStr + "_end_index").val(str);

		$("#txt_" + typeStr + "_start_index").attr("data", str);
		$("#txt_" + typeStr + "_end_index").attr("data", str);
	}
	if(isTranscriptPoint) {
		var ddd = "tr[id='" + trid + "'] #" + type + "_score";
		var scorestr = $(ddd).html();
		if(scorestr) {
			var oldscore = parseFloat(scorestr.replace("每题", ""));
			$("#txt_" + typeStr + "_score").val(oldscore);
		}
	}

	if(type == "SingleChoice") {
		//rdo_MultiChoice_sin
		$("#rdo_" + type + "_sin").prop("checked", true);
	}
	if(type == "MultiChoice") {
		$("#rdo_" + type + "_mul").prop("checked", true);
	}
};

TopicCommonModule.prototype.offsetEvent = function() {
	var _this = this;

	function hidePop() {
		$("#pop_offset").hide();
		$("#showMask").hide();
		$("#txt_offset_name").val("");
	}

	$("#btn_offset_close,#btn_offset_cancel").on("click", function() {
		hidePop();
	});

	$("#btn_offset").on("click", function() {
		$("#showMask").show();
		_this.clearMsg();
		$("#pop_offset").show();
	});
	$("#btn_offset_submit").unbind('click');
	$("#btn_offset_submit").click(function() {
		var newOffset = $("#txt_offset_name").val();
		newOffset = newOffset.replace(/\s/g, "");
		if(!/^(-)?[0-9]{1,3}$/g.test(newOffset)) {
			$("span.msg").html("请输入3位数以内的正数或负数！");
			return;
		}
		var topicTypes = DescCtrl.getTopicTypeDescs();
		var minNo;
		if(topicTypes.length > 0 && topicTypes[0].topicDescs.length > 0) {
			minNo = topicTypes[0].topicDescs[0].no;
			if(parseInt(newOffset) + parseInt(minNo) <= 0) {
				$("span.msg").html("偏移量不符和规范，请重新输入！");
				return;
			}
		}

		if($.trim(newOffset).length > 0) {
			DescCtrl.setNoOffset(newOffset);
			GlobalCtrl.saveFrontJson();
			GlobalCtrl.drawAnswerSheet();
		}
		hidePop();
	});

};

/**
 * 获取创建题目基本信息
 */
TopicCommonModule.prototype.checkTopicCondition = function() {
	this.topicName = $.trim($("#txt_" + this.typeId + "_name").val());
	this.topicStartIndex = $.trim($("#txt_" + this.typeId + "_start_index").val());
	//选项个数
	this.topicSelectCount = $.trim($("#txt_" + this.typeId + "_select_count").val());
	this.score = $.trim($("#txt_" + this.typeId + "_score").val());

	this.score = parseInt(this.score);
	this.dataTag = $.trim($("#pop_" + this.typeId).attr("dataTag")); //on 标记拆分题目
	//填空题每行列数 ;英语作文行数；语文作文格子数
	this.colnum = $.trim($("#txt_" + this.typeId + "_select_count").val());
	//如果不是创建整个TopicType
	if((this.typeId == "Object")) {
		//需要知道添加题目的类型，是单选题还是多选题
		if($('input[name=objectType_' + this.typeId + ']').get(0).checked) {
			this.addSmallTopicType = "SingleChoice";
		} else {
			this.addSmallTopicType = "MultiChoice";
		}
	}

	//if ((this.typeId == "FillBlank")) {
	//    //誊分模式 自制全部带框，带有学科的只有英语允许不带框
	//    //非誊分模式全部不带框
	//    if ($('input[name=fillBlank_type]').get(0).checked) {
	//        this.addSmallTopicType = "FrameBlank";
	//    } else {
	//        this.addSmallTopicType = "PureBlank";
	//    }
	//}

	//如果不是创建整个TopicType
	if(!this.isCreateTopicType && (this.typeId == "SingleChoice" || this.typeId == "MultiChoice" || this.typeId == "Object")) {
		//需要知道添加题目的类型，是单选题还是多选题
		if($('input[name=objectType_' + this.typeId + ']').get(0).checked == true) {
			this.addTypeId = "SingleChoice";
		} else {
			this.addTypeId = "MultiChoice";
		}
	}

	//如果是作文题，则无需结束题号(结束题号等于开始题号)。
	if(this.typeId == "Composition" || this.typeId == "EnglishComposition") {
		this.topicEndIndex = this.topicStartIndex;
	} else {
		this.topicEndIndex = $.trim($("#txt_" + this.typeId + "_end_index").val());
	}
	if(this.typeId == "AnswerQuestion" || this.typeId == "Composition" || this.typeId == "EnglishComposition" || this.typeId == "FillBlank") {
		this.BoxType = 0;
		this.isDecimal = 0;
		if($("#cbk_" + this.typeId + "_decimal")) {
			if($("#cbk_" + this.typeId + "_decimal").is(':checked')) {
				this.isDecimal = 1;
			}
		}
		var scoBox = $('input[name=' + this.typeId + '_type]');
		//是否显示 打分框 
		if(scoBox.get(1).checked) {
			this.BoxType = 1;
		} else {
			if(scoBox.length > 4) {
				if(scoBox.get(2).checked) {
					this.BoxType = 2;
				} else if(scoBox.get(3).checked) {
					this.BoxType = 3;
				} else if(scoBox.get(4).checked) {
					this.BoxType = 4;
				}
			} else if(scoBox.length > 3) {
				if(scoBox.get(2).checked) {
					this.BoxType = 2;
				} else if(scoBox.get(3).checked) {
					this.BoxType = 3;
				}
			} else if(scoBox.length > 2) {
				if(scoBox.get(2).checked) {
					this.BoxType = 2;
				}
			}
		}

		if(this.BoxType != 0) {
			if(this.score == "") {
				return window.Util.Dialogue.dialogBox(true, "温馨提示", "请输入满分值!");
			} else if(isInteger(this.score) == false) {
				return window.Util.Dialogue.dialogBox(true, "温馨提示", "满分值只能是整数!");
			} else if(this.BoxType == 3 && this.score >= 10) {
				return window.Util.Dialogue.dialogBox(true, "温馨提示", "对错框题目分数只能是10分以下!");
			}

		}
	}

	if(this.dataTag == "on") {
		this.isCreateTopicType = true;
		//this.showAnswerQuestionDesc = $("#ck_showAnswerQuestion")[0].checked;
	}

	//数据格式判断
	var formatResult = this.checkTextFormat();
	if(formatResult) {
		$("span.msg").html(formatResult);
		return;
		//return window.Util.Dialogue.dialogBox(true, "提示框", formatResult);
	}
	// 编辑作文题字数
	if(this.typeId == "Composition") {
		var topicId = $("#h_Composition_topicId").val();
		if(topicId) {
			DescCtrl.updateCompositionCount(this.typeId, topicId, this.topicSelectCount, this.score);
			this.popHide();
			this.operationEndFn();
			return;
		}
	}
	/*if (this.typeId == "EnglishComposition"){
	 var topicId = $("#h_EnglishComposition_topicId").val();
	 if (topicId){
	 DescCtrl.updateCompositionCount(topicId, this.topicSelectCount,this.score);
	 this.popHide();
	 this.operationEndFn();
	 return;
	 }
	 }*/
	//拆分题目
	if(this.dataTag == "on") {
		//this.showQuestionDesc = $("#ck_showQuestionTitle_" + this.typeId)[0].checked;
		//if ((this.typeId == "FillBlank")) {
		//    if (DescCtrl.getAnswerSheetModel() && subjectCode == "03") {
		//        var id = $("input:radio[name='fillBlank_type']:checked").attr("id");
		//        if (id == "rdo_FillBlank_pure") {
		//            this.showQuestionDesc = false;
		//        }
		//    }
		//}

		/*if(this.typeId != "SingleChoice" && this.typeId != "MultiChoice" && this.typeId != "Object"){
		 this.showQuestionDesc = $("#ck_showQuestionTitle_"+this.typeId)[0].checked;
		 }else{
		 this.showQuestionDesc = false;//
		 }*/

		var min = parseInt($("#txt_" + this.typeId + "_start_index").attr("data"));
		var max = parseInt($("#txt_" + this.typeId + "_end_index").attr("data"));
		//判断最大值最小值
		if(this.topicStartIndex < min || this.topicEndIndex > max) {
			//return window.Util.Dialogue.dialogBox(true, "温馨提示", "题号超出范围，请更改，或添加题目！");
			$("span.msg").html("题号超出范围，请更改，或添加题目！");
			return;
		}
		this.splitTopicList(this.typeId, this.addSmallTopicType, this.topicStartIndex, this.topicEndIndex, this.score, this.colnum, this.topicName, this.showQuestionDesc, this.BoxType); //
		return;
	}

	//题号不支持重复。全局所有小题不支持重复 checkAllRepeat
	var checkTopicIsRepeat = this.checkAllRepeat();
	if(checkTopicIsRepeat) { //&& !(this.typeId == "Composition" || this.typeId=="EnglishComposition")
		return window.Util.Dialogue.dialogBox(true, "温馨提示", checkTopicIsRepeat);
	}

	if(!this.isCreateTopicType) {

		var checkTopicIsRepeat = this.checkTextIsRepeat();
		if(checkTopicIsRepeat) {
			return window.Util.Dialogue.dialogBox(true, "提示框", checkTopicIsRepeat);
		}
		this.createTopic();
	} else {
		this.createTopicType();
	}
};

/**
 * 创建大题型，内部的题目会自动创建
 */
TopicCommonModule.prototype.createTopicType = function() {
	//if(DescCtrl.answerSheetDesc.isTranscriptsPoints){
	var subjectiveCategory = "";

	if(this.typeId == "AnswerQuestion") {
		subjectiveCategory = "AnswerQuestion";
	}

	if(this.typeId == "EnglishComposition") {
		if(!DescCtrl.getAnswerSheetModel()) {
			this.pCategory = "AnswerQuestion";
		} else {
			this.pCategory = "EnglishComposition";
		}
		subjectiveCategory = "EnglishComposition";
		this.addSmallTopicType = "EnglishComposition";
	}
	if(this.typeId == "Object" || this.typeId == "SingleChoice" || this.typeId == "MultiChoice") {
		this.addSmallTopicType = this.typeId;
		subjectiveCategory = this.typeId;
	}
	if(this.typeId == "FillBlank") {
		subjectiveCategory = "FillBlank";
	}
	//添加初始化数据
	DescCtrl.addTopicType(this.topicName,
		parseInt(this.topicStartIndex),
		parseInt(this.topicEndIndex),
		parseInt(this.topicSelectCount ? this.topicSelectCount : 1), //optionCount
		parseInt(this.colnum), //colCount
		this.pCategory ? this.pCategory : this.typeId,
		subjectiveCategory,
		this.addSmallTopicType,
		parseFloat(this.score),
		this.BoxType,
		this.isDecimal);

	/*}else{
	 var subjectiveCategory = "";
	 if(this.typeId == "EnglishComposition"){
	 subjectiveCategory = "EnglishComposition";
	 }
	 if(this.typeId == "FillBlank"){
	 subjectiveCategory = "FillBlank";
	 //DescCtrl.addTopicTypeWith1topicDesc(this.topicName, parseInt(this.topicStartIndex), parseInt(this.topicEndIndex), parseInt(this.topicSelectCount), parseInt(this.topicSelectCount), this.pCategory ? this.pCategory : this.typeId,subjectiveCategory);
	 }else{
	 //DescCtrl.addTopicType(this.topicName, parseInt(this.topicStartIndex), parseInt(this.topicEndIndex), parseInt(this.topicSelectCount), parseInt(this.topicSelectCount), this.pCategory ? this.pCategory : this.typeId,subjectiveCategory);
	 }
	 DescCtrl.addTopicType(this.topicName, parseInt(this.topicStartIndex), parseInt(this.topicEndIndex), parseInt(this.topicSelectCount), parseInt(this.topicSelectCount), this.pCategory ? this.pCategory : this.typeId,subjectiveCategory,this.addSmallTopicType);

	 }*/
	this.popHide();
	this.operationEndFn();
	//this.baseBindEvent();
};

/**
 * 在某个题型中添加小题
 */
TopicCommonModule.prototype.createTopicInit = function(topicTypeDescId, topicId) {
	this.isCreateTopicType = false;
	this.topicTypeDescId = topicTypeDescId;
	//当前小题id
	this.topicId = topicId || "";
	this.setPopData();
	this.popShow();
	this.clearMsg();
};

/**
 * 在某个题型中添加小题
 */
TopicCommonModule.prototype.createTopic = function() {
	//if (this.typeId == "FillBlank") {
	//    DescCtrl.addFillBlankTopic(this.topicTypeDescId, parseInt(this.topicStartIndex), parseInt(this.topicEndIndex), parseInt(this.topicSelectCount), this.typeId, this.topicId, this.score);
	//} else {
	DescCtrl.addTopic(this.topicTypeDescId, parseInt(this.topicStartIndex), parseInt(this.topicEndIndex), parseInt(this.topicSelectCount), this.addTypeId, this.topicId, this.score, this.isDecimal);
	//}
	//DescCtrl.addTopic(this.topicTypeDescId, parseInt(this.topicStartIndex), parseInt(this.topicEndIndex), parseInt(this.topicSelectCount), this.addTypeId,this.topicId,this.score);

	this.popHide();
	this.operationEndFn();
};

/**
 * 拆分题目列表
 */
TopicCommonModule.prototype.splitTopicList = function(typeId, smallTopic, topicStartIndex, topicEndIndex, score, column, topicName, showAnswerQuestionDesc, BoxType) {
	var objTopicType = [];
	if(!parseFloat(score)) {
		score = 0;
	} else {
		score = parseFloat(score);
	}
	var subCate = smallTopic || "AnswerQuestion";
	if(typeId == "FillBlank" && (smallTopic == "FrameBlank" || smallTopic == "PureBlank")) {
		typeId = "AnswerQuestion";
		subCate = "FillBlank";
	}
	if(typeId == "EnglishComposition") {
		typeId = "EnglishComposition";
		subCate = "EnglishComposition";
	}
	if(smallTopic == "AnswerQuestion" || smallTopic == "Judge") {
		subCate = "";
	}
	if(typeId == "Composition") {
		subCate = "";
	}
	if(typeId == "SingleChoice" || typeId == "MultiChoice") {
		//typeId == "Object" ||
		subCate = "";
		objTopicType = DescCtrl.getTopicTypeDescs("Object");
	}
	//对于客观题在题目列表中题目进行了拆分，这里查找会找不到。
	var topicTypeDescs = DescCtrl.getTopicTypeDescs(typeId, subCate).concat(objTopicType);
	var pTypeNo, needSplit = false;
	var isCross = true; //是否跨题目
	var newTopicArr = [];
	//题干控制单独处理：不要验证题号：
	for(var n = 0; n < topicTypeDescs.length; n++) {
		topicTypeDescs[n].BoxType = BoxType;
		var topDesc = topicTypeDescs[n].topicDescs;
		if(topicTypeDescs[n].category == "AnswerQuestion" && topicTypeDescs[n].subjectiveCategory == "AnswerQuestion") {
			for(var j = 0; j < topDesc.length; j++) {
				topDesc[j].BoxType = BoxType;
			}
		}

		if(topicTypeDescs[n].showQuestionStem != showAnswerQuestionDesc) {
			for(var j = 0; j < topDesc.length; j++) {
				if(subCate == "FillBlank") {
					topicTypeDescs[n].colCount = 1;
					topDesc[j].smallTopicType = "FrameBlank";
					topDesc[j].height = 0;
				}
				topDesc[j].content = ""; //重绘制
				topDesc[j].nextContent = "";
				topDesc[j].showQuestionDesc = showAnswerQuestionDesc;
			}
			topicTypeDescs[n].showQuestionStem = showAnswerQuestionDesc;
		}
	}

	for(var n = 0; n < topicTypeDescs.length; n++) {

		var topDesc = topicTypeDescs[n].topicDescs;
		//合并题目情况
		if(!(topicEndIndex <= topicTypeDescs[n].endNo && topicStartIndex >= topicTypeDescs[n].beginNo)) {
			continue;
			//return window.Util.Dialogue.dialogBox(true, "温馨提示", "抱歉，暂不支持合并处理题目。");
		}
		//题号不连续也不支持,会导致丢题topicDescs.splice
		if(topicTypeDescs[n].topicDescs.length < (topicEndIndex - topicStartIndex + 1)) {
			continue;
		}

		isCross = false;

		//作文题和解答题处理
		if(typeId == "EnglishComposition" && subCate == "EnglishComposition" && topDesc[0].no == topicStartIndex) {

			if(topDesc[0].score != score) {
				topDesc[0].content = "";
				topDesc[0].nextContent = "";
			}

			var p = /、（.*?分）/;
			var ch = decodeURIComponent(topDesc[0].content);
			var ht = ch.replace(p, '、（' + score + '分）');
			topDesc[0].content = encodeURI(ht);
			topDesc[0].score = score;

			//修改答案中分数,修改更新的那些题目
			$.each(DescCtrl.answerDescs, function(i, v) {
				if(v.topicId == topDesc[0].id) {
					v.score = score;
				}
			});
			break;
		}

		//去掉题目中的(单/多)
		//topicName = topicName.replace("（单）", "").replace("（多）", "");
		if(subCate != "FillBlank") {
			topicTypeDescs[n].title = topicName;
		}

		for(var j = 0; j < topDesc.length; j++) {
			if((topDesc[j].no >= topicStartIndex && topDesc[j].no <= topicEndIndex) || (topDesc[j].noList[0] >= topicStartIndex && topDesc[j].noList[0] <= topicEndIndex)) {
				//var oldScore = topDesc[j].score;
				pTypeNo = n;
				topDesc[j].score = score;

				//題目中（）里的分數，解答题和作文题
				var p = /、（.*?分）/;
				var ch = decodeURIComponent(topDesc[j].content);
				var ht = ch.replace(p, '、（' + score + '分）');
				topDesc[j].content = encodeURI(ht);
				var ch = decodeURIComponent(topDesc[j].questionContent);
				var ht = ch.replace(p, '、（' + score + '分）');
				topDesc[j].questionContent = encodeURI(ht);

				//修改答案中分数,修改更新的那些题目
				$.each(DescCtrl.answerDescs, function(i, v) {
					if(v.topicId == topDesc[j].id) {
						v.score = score;
					}
				});

				//更改填空题目类型后，题目进行拆分
				//|| showAnswerQuestionDesc != topicTypeDescs[n].showQuestionStem
				if(topicTypeDescs[n].subjectiveCategory == "FillBlank" && (topDesc[j].smallTopicType != smallTopic || topicTypeDescs[n].colCount != column)) {
					topDesc[j].height = "";
					needSplit = true;
					topDesc[j].smallTopicType = smallTopic;
					newTopicArr.push(topDesc[j]);
				}

				/*//选择题外的题干处理，选择题本期不支持
				 if(typeId != "Judge" && typeId != "Object" &&typeId != "SingleChoice" &&typeId != "MultiChoice"){
				 //填空题显示题干的话强制转为带对错框；非誊分模式不变不带框
				 //强制每行一题，不可逆操作。
				 if(topDesc[j].showQuestionDesc != showAnswerQuestionDesc ){
				 if(subCate == "FillBlank"){
				 topicTypeDescs[n].colCount = 1;
				 topDesc[j].smallTopicType = "FrameBlank";
				 }
				 topDesc[j].content="";//重绘制
				 topDesc[j].nextContent = "";
				 }
				 topDesc[j].showQuestionDesc = showAnswerQuestionDesc;
				 }
				 //最后更改大题的题干显示与否
				 if(j==topDesc.length-1){
				 topicTypeDescs[n].showQuestionStem = showAnswerQuestionDesc;
				 }*/

			}
		}
	}

	if(isCross) {
		$("span.msg").html("抱歉，暂不支持跨题操作！");
		return;
	}

	//从topDesc中删除newarr对应的题
	if(needSplit) {

		var blankType = newTopicArr[0].smallTopicType;
		var t = topicTypeDescs[pTypeNo].title;
		if(blankType == "PureBlank") {
			t = t.replace("（仅对的打√）", "");
		} else if(blankType == "FrameBlank" && t.indexOf("×") < 0 && DescCtrl.answerSheetDesc.isTranscriptsPoints) { //誊分卡
			t += "（仅对的打√）";
		}

		var colCount = column; //topicTypeDescs[pTypeNo].colCount;
		if(showAnswerQuestionDesc) {
			colCount = 1;
		}
		var topicTypeId = UUID.generate();
		var newTypeDesc = new desc.TopicTypeDesc(topicTypeId, t, parseInt(topicStartIndex), colCount, newTopicArr.length, colCount, typeId, newTopicArr, parseInt(topicEndIndex), subCate);
		newTypeDesc.showQuestionStem = topicTypeDescs[pTypeNo].showQuestionStem;
		var splitIndex = TopicCommonModule.prototype.getIndexbyNo(topicTypeDescs[pTypeNo].topicDescs, topicStartIndex);
		topicTypeDescs[pTypeNo].topicDescs.splice(splitIndex, topicEndIndex - topicStartIndex + 1);

		//中间拆分还是前后拆分，拆分成两段还是三段
		//两段
		if(splitIndex == 0) {
			//前面拆
			topicTypeDescs[pTypeNo].beginNo = topicTypeDescs[pTypeNo].beginNo + newTopicArr.length;
			DescCtrl.answerSheetDesc.topicTypeDescs.unshift(newTypeDesc);
		}
		if(splitIndex > 0 && (splitIndex + parseInt(topicEndIndex) - parseInt(topicStartIndex) + 1 == topicTypeDescs[pTypeNo].topicDescs.length + newTopicArr.length)) {
			//后面拆
			topicTypeDescs[pTypeNo].endNo = topicTypeDescs[pTypeNo].endNo - newTopicArr.length;
			DescCtrl.answerSheetDesc.topicTypeDescs.push(newTypeDesc);
		}
		topicTypeDescs[pTypeNo].count -= topicEndIndex - topicStartIndex + 1;

		if(splitIndex > 0 && (splitIndex + parseInt(topicEndIndex) - parseInt(topicStartIndex) + 1 < topicTypeDescs[pTypeNo].topicDescs.length + newTopicArr.length)) {

			DescCtrl.answerSheetDesc.topicTypeDescs.unshift(newTypeDesc);

			//中间拆三段，剩下的两段分开，前面一段拿出来新建，后面一段改beginNo。
			var frontArr = topicTypeDescs[pTypeNo].topicDescs.splice(0, splitIndex);
			var colCount = topicTypeDescs[pTypeNo].colCount;
			var topicTypeIdfront = UUID.generate();
			var newTypeDescFront = new desc.TopicTypeDesc(topicTypeIdfront, topicTypeDescs[pTypeNo].title, frontArr[0].no, colCount, frontArr.length, colCount, typeId, frontArr, frontArr[frontArr.length - 1].no, subCate);
			DescCtrl.answerSheetDesc.topicTypeDescs.unshift(newTypeDescFront);

			topicTypeDescs[pTypeNo].beginNo = topicTypeDescs[pTypeNo].topicDescs[0].no;
			topicTypeDescs[pTypeNo].count -= frontArr.length;

		}

	}
	//清空没有小题的大题
	$.each(DescCtrl.answerSheetDesc.topicTypeDescs, function(i, v) {
		if(v && v.topicDescs.length == 0) {
			DescCtrl.answerSheetDesc.topicTypeDescs.splice($.inArray(v, DescCtrl.answerSheetDesc.topicTypeDescs), 1);
		}
	});

	//排序DescCtrl.homeworkSheetDesc.topicTypeDescs
	DescCtrl.answerSheetDesc.topicTypeDescs.sort(function(a, b) {
		return a.beginNo - b.beginNo
	});
	this.popHide();
	this.operationEndFn();
	//this.baseBindEvent();
};

/**
 * 删除题目（按类型删除）
 */
TopicCommonModule.prototype.deleteTopicByTopicType = function() {
	if(this.pCategory) {
		DescCtrl.delTopicByCategory(this.pCategory, this.typeId);
	} else {
		DescCtrl.delTopicByCategory(this.typeId);
	}
	this.operationEndFn();
};

/**
 * 删除题目(按TopicTypeDescId删除)
 */
TopicCommonModule.prototype.deleteTopicByTopicTypeDescId = function(type, topicTypeDescId) {
	this.deleteAnswerOfTopicTypeDesc(type, topicTypeDescId);
	DescCtrl.delTopicByTopicTypeDescId(this.typeId, topicTypeDescId);

	this.delTopicList();
	this.operationEndFn();
};

/**
 * 删除题目同时删除对应的答案
 */
TopicCommonModule.prototype.deleteAnswerOfTopicTypeDesc = function(typeId, topicTypeDescId) {
	var start = 0;
	var end = 0;
	if(typeId == "Object") {
		var topicDescs = DescCtrl.getTopicTypeDescs("Object");
		if(topicDescs.length != 0) {
			start = topicDescs[0].beginNo;
			end = topicDescs[0].endNo;
		}
	} else if(typeId == "FillBlank") {
		//填空题在非誊分模式下删除小题：topicTypeDescId
		var topicDesc = DescCtrl.getTopicTypeDesc(topicTypeDescId);
		if(topicDesc) {
			start = topicDesc.beginNo;
			end = topicDesc.endNo;
		} else {
			var topicDesc = DescCtrl.getTopicDesc(topicTypeDescId);
			start = end = topicDesc.noList[0];
		}

	} else if(typeId == "SingleChoice") {
		var topicDesc = DescCtrl.getTopicTypeDesc(topicTypeDescId);
		//start = parseInt(this.topicStartIndex);
		//end = parseInt(this.topicEndIndex);
		start = topicDesc.beginNo;
		end = topicDesc.endNo;
	} else if(typeId == "MultiChoice") {
		var topicDesc = DescCtrl.getTopicTypeDesc(topicTypeDescId);
		//start = parseInt(this.topicStartIndex);
		//end = parseInt(this.topicEndIndex);
		start = topicDesc.beginNo;
		end = topicDesc.endNo;
	} else if(typeId == "Composition") {
		var topicDesc = DescCtrl.getTopicDesc(topicTypeDescId, typeId);
		start = end = topicDesc.noList[0];
	} else {
		var topicDesc = DescCtrl.getTopicDesc(topicTypeDescId);
		start = end = topicDesc.noList[0];
	}

	if(!DescCtrl.answerDescs || DescCtrl.answerDescs.length == 0) {
		return;
	}
	for(var i = start; i <= end; i++) {
		DescCtrl.delAnswerDesc(i);
	}

};

/**
 * 删除相应类型的题目列表内容
 */
TopicCommonModule.prototype.delTopicList = function() {
	$("#table_topic_detail tr[name='" + this.addSmallTopicType + "']").remove();
	$("#table_topic_detail tr[name='" + this.typeId + "']").remove();
	if(this.typeId == "Object") {
		$("#table_topic_detail tr[name='MultiChoice']").remove();
		$("#table_topic_detail tr[name='SingleChoice']").remove();
	}
};

/**
 * 操作后重新绘制数据
 */
TopicCommonModule.prototype.operationEndFn = function() {
	this.saveFrontJson();
	this.resetShowQuestionDesc();
	//渲染界面
	this.drawAnswerSheet();
	//右边的显示
	this.initDataView();
	this.clearPopData();
};

/**
 * 实时保存创建JSON数据
 */
TopicCommonModule.prototype.saveFrontJson = function() {
	GlobalCtrl.saveFrontJson();
};

/**
 * 渲染界面
 */
TopicCommonModule.prototype.drawAnswerSheet = function() {
	GlobalCtrl.drawAnswerSheet();
};

/**
 * 更新答题卡总分
 */
TopicCommonModule.prototype.updateTotalScore = function(score) {
	//显示答案表中的总分
	$(".totalScore b.tscore").html(score);
};

TopicCommonModule.prototype.calculateTotalScore = function() {
	var totalScore = 0;
	var anlist = DescCtrl.getAnswerDescs();
	if(anlist) {
		for(x in anlist) {
			totalScore += parseFloat(anlist[x].score);
		}
	}
	return totalScore.toFixed(2);
};
/**
 * 清空列表重新全部添加
 * 重新显示题目数量（右侧table中）
 */
TopicCommonModule.prototype.initDataView = function() {
	$("#table_topic_detail tr").remove();
	var topicTypeDescs = DescCtrl.getTopicTypeDescs();
	if(topicTypeDescs.length > 0) {
		$("#table_topic").show();
		this.initDataViewDetailAll(DescCtrl.answerSheetDesc.isTranscriptsPoints);
		if(DescCtrl.answerSheetDesc.isTranscriptsPoints) {
			$(".totalScore").show();
			$(".topicNum").hide();
			var t = this.calculateTotalScore();
			$(".totalScore b.tscore").html(t);
			$("#table_topic_detail tr td").show();
			$(".dtk_right_box_tab table#table_topic_detail td").css("cursor", "pointer");
		} else {
			$(".totalScore").hide();
			$(".topicNum").show();
			$("#table_topic_detail tr td[name*=_score]").hide();
			$(".dtk_right_box_tab table#table_topic_detail td").css("cursor", "default");
		}
		//$("table#table_topic_detail tr td").show();
	} else {
		$("#table_topic").hide();
	}
};

/**
 * 全部重新添加
 * @param pCategory
 * @param typeId
 * @param isTranscriptPoint
 */
TopicCommonModule.prototype.initDataViewDetailAll = function(isTranscriptPoint) {
	var titleScoreNumRowsObject0 = DescCtrl.getTitleScoreNumListForObject("Object", "SingleChoice");
	//this.showTitleScoreNumRows(titleScoreNumRowsObject0,this.typeId,isTranscriptPoint);

	var titleScoreNumRowsObject = DescCtrl.getTitleScoreNumListForObject("SingleChoice");
	var titleScoreNumRows = this.mergeNumRows(titleScoreNumRowsObject0, titleScoreNumRowsObject, isTranscriptPoint);
	this.showTitleScoreNumRows(titleScoreNumRows, this.typeId, isTranscriptPoint);

	var titleScoreNumRowsObject0 = DescCtrl.getTitleScoreNumListForObject("Object", "MultiChoice");
	//this.showTitleScoreNumRows(titleScoreNumRowsObject0,this.typeId,isTranscriptPoint);

	var titleScoreNumRowsObject = DescCtrl.getTitleScoreNumListForObject("MultiChoice");
	var titleScoreNumRows = this.mergeNumRows(titleScoreNumRowsObject0, titleScoreNumRowsObject, isTranscriptPoint);
	this.showTitleScoreNumRows(titleScoreNumRows, this.typeId, isTranscriptPoint);

	var titleScoreNumRows = DescCtrl.getTitleScoreNumberNotObject("AnswerQuestion", "AnswerQuestion");
	this.showTitleScoreNumRows(titleScoreNumRows, this.typeId, isTranscriptPoint);

	var titleScoreNumRows = DescCtrl.getTitleScoreNumberNotObject("AnswerQuestion", "FillBlank");
	this.showTitleScoreNumRows(titleScoreNumRows, this.typeId, isTranscriptPoint);

	var titleScoreNumRows = DescCtrl.getTitleScoreNumberNotObject("Composition");
	this.showTitleScoreNumRows(titleScoreNumRows, this.typeId, isTranscriptPoint);

	if(DescCtrl.getAnswerSheetModel()) {
		var titleScoreNumRows = DescCtrl.getTitleScoreNumberNotObject("EnglishComposition");
		this.showTitleScoreNumRows(titleScoreNumRows, this.typeId, isTranscriptPoint);
	} else {
		var titleScoreNumRows = DescCtrl.getTitleScoreNumberNotObject("AnswerQuestion", "EnglishComposition");
		this.showTitleScoreNumRows(titleScoreNumRows, this.typeId, isTranscriptPoint);
	}

};
/*
 TopicCommonModule.prototype.initDataViewDetail = function(pCategory,typeId,isTranscriptPoint){
 //某一种特定类型题目，在右边列表中显示出一行
 if(this.typeId == "Object" ||this.typeId == "SingleChoice" || this.typeId == "MultiChoice"){
 var titleScoreNumRowsObject = DescCtrl.getTitleScoreNumListForObject("Object");
 this.showTitleScoreNumRows(titleScoreNumRowsObject,this.typeId,isTranscriptPoint);

 var titleScoreNumRowsObject = DescCtrl.getTitleScoreNumListForObject("MultiChoice");
 this.showTitleScoreNumRows(titleScoreNumRowsObject,this.typeId,isTranscriptPoint);

 var titleScoreNumRowsObject = DescCtrl.getTitleScoreNumListForObject("SingleChoice");
 this.showTitleScoreNumRows(titleScoreNumRowsObject,this.typeId,isTranscriptPoint);
 }else {
 var titleScoreNumRows;
 if (pCategory) {
 titleScoreNumRows = DescCtrl.getTitleScoreNumberNotObject(pCategory, typeId);
 }else{
 titleScoreNumRows = DescCtrl.getTitleScoreNumberNotObject(typeId);
 }
 this.showTitleScoreNumRows(titleScoreNumRows,this.typeId,isTranscriptPoint);
 }
 };*/

TopicCommonModule.prototype.mergeNumRows = function(titleScoreNumRows1, titleScoreNumRows2, isTranscriptPoint) {
	var reSult = [];
	var titleScoreNumRows = titleScoreNumRows1.concat(titleScoreNumRows2);
	titleScoreNumRows.forEach(function(row) {
		var pos;
		var isRepeat = false;

		var i = 0;
		for(; i < reSult.length; i++) {
			//reSult.forEach(function (oriRow) {
			if(isTranscriptPoint) {
				//誊分模式区分分数
				if(reSult[i].category == row.category && reSult[i].score == row.score) {
					isRepeat = true;
					pos = i;
				}
			} else {
				//非誊分模式类型一样就合并
				if(reSult[i].category == row.category) {
					isRepeat = true;
					pos = i;
				}
			}
		}

		if(!isRepeat) {
			reSult.push(row);
		} else {
			if(isTranscriptPoint) {
				//oriRow.numList += ",<br>"+ row.numList;
				reSult[pos].numList += ",<br>" + row.numList;
			} else {
				reSult[pos].numList += "," + row.numList;
			}
		}

	});
	return reSult;
};

TopicCommonModule.prototype.showTitleScoreNumRows = function(titleScoreNumRows, typeId, isTranscriptPoint) {
	if(titleScoreNumRows && titleScoreNumRows.length > 0) {
		for(var i = 0; i < titleScoreNumRows.length; i++) {
			var rowId = "titleScoreNumRow_" + titleScoreNumRows[i].category + "_" + titleScoreNumRows[i].subCategory + "_" + titleScoreNumRows[i].firstNum + "_" + titleScoreNumRows[i].title + "_" + titleScoreNumRows[i].score;
			//$("#" + rowId).remove();
			var nameStr = titleScoreNumRows[i].category;
			if(titleScoreNumRows[i].subCategory) {
				nameStr = titleScoreNumRows[i].subCategory;
			}
			//四舍五入显示分数
			var sc = titleScoreNumRows[i].score;
			if(titleScoreNumRows[i].score && titleScoreNumRows[i].score.toString().split(".").length > 1 && titleScoreNumRows[i].score.toString().split(".")[1].length > 2) {
				sc = titleScoreNumRows[i].score.toFixed(2);
			}

			var newTitleScoreNumRow = '<tr name="' + nameStr + '" id="' + rowId + '">' +
				//'<span name = "'+ titleScoreNumRows +'" hidden></span>'+//选项个数
				'<td width=30% id = "' + nameStr + '_name" name = "' + nameStr + '_name">' + titleScoreNumRows[i].title + '</td>' +
				'<td width=28% id = "td_' + nameStr + '_count" name = "td_' + nameStr + '_count" style="word-wrap:break-word;word-break:break-all;">' + titleScoreNumRows[i].numList + '</td>';
			//'<td width=45% id = "'+nameStr+'_score" name = "'+nameStr+'_score" >每题'+ sc + '分</td></tr>';
			if(isTranscriptPoint) {
				newTitleScoreNumRow += '<td width=42% id = "' + nameStr + '_score" name = "' + nameStr + '_score" >每题' + sc + '分</td>';
			} else {
				newTitleScoreNumRow += '<td width=42% id = "' + nameStr + '_score" name = "' + nameStr + '_score" style="display: none">每题' + sc + '分</td>';
			}
			newTitleScoreNumRow += '</tr>';
			//找到适当的位置，插入
			var sortStandard = {
				SingleChoice_SingleChoice: 1,
				MultiChoice_MultiChoice: 2,
				Object_Object: 3,
				AnswerQuestion_FillBlank: 4,
				AnswerQuestion_AnswerQuestion: 5,
				AnswerQuestion_EnglishComposition: 6,
				Composition: 7
			};

			var rowCount = $("#table_topic_detail tr").length;
			//如果之前是空的就，直接添加上
			if(rowCount == 0) {
				$("#table_topic_detail").append(newTitleScoreNumRow);
			} else {
				var index = 0;
				//找到合适的位置直接插入进去
				$('#table_topic_detail tr').each(function() {
					index++;
					var rowId = $(this).attr("id");
					var rowIdType = rowId.split('_')[1] + "_" + rowId.split('_')[2];
					//如果遇到某一个比当前的大，就将当前的插入前面
					if(sortStandard[rowIdType] > sortStandard[titleScoreNumRows[i].category + "_" + titleScoreNumRows[i].subCategory]) {
						//直接插到前面
						$(this).before(newTitleScoreNumRow);
						return false; //跳出each
					}

					if(index == rowCount) {
						$(this).after(newTitleScoreNumRow);
						return false; //跳出each
					}
				});
			}
		}
	}
	/*else {
	 //清楚对应的列表 typeId
	 $("tr[name="+typeId+"]").remove();
	 }*/
};

/* 原来的题目列表
 TopicCommonModule.prototype.initDataViewDetail = function(doTypeId){
 var topicNumber = DescCtrl.getTopicNumberByCategory(doTypeId);
 if(this.pCategory){
 topicNumber = DescCtrl.getTopicNumberByCategory(this.pCategory,doTypeId);
 }

 if (topicNumber && topicNumber.length > 0) {
 $("#tr_" + doTypeId).show();
 $("#td_" + doTypeId + "_count").html(topicNumber);
 //$("#td_" + doTypeId + "_delete_div").show();
 } else {
 $("#tr_" + doTypeId).hide();
 }
 };*/

/**
 * 验证弹出框文本区域数据格式
 */
TopicCommonModule.prototype.checkTextFormat = function() {
	var isEmpty = this.checkTextIsEmpty();
	if(isEmpty) {
		return isEmpty;
	}
	var isDigit = this.checkTextIsDigit();
	if(isDigit) {
		return isDigit;
	}
	var overflow = this.checkTextIsOverflow();
	if(overflow) {
		return overflow;
	}
	var isLtMaxCount = this.checkLtMaxCount();
	if(isLtMaxCount) {
		return isLtMaxCount;
	}
	return "";
};

/**
 * 验证弹出框文本区域数据是否超出最大值
 */
TopicCommonModule.prototype.checkTextIsRepeat = function() {
	var checkTopicIsRepeat = this.checkIsRepeat();
	if(checkTopicIsRepeat) {
		return checkTopicIsRepeat;
	}
	return "";
};

/**
 * 判断是否添加重复小题题号
 */
TopicCommonModule.prototype.checkAllRepeat = function() {
	var result = DescCtrl.checkAllIsRepeat(parseInt(this.topicStartIndex), parseInt(this.topicEndIndex));
	return result ? "" : errorMessage.TopicIsRepeat;
};

/**
 * 判断文本框是否为空
 */
TopicCommonModule.prototype.checkTextIsEmpty = function() {
	if(!this.topicName) {
		return errorMessage.topicNameIsEmpty;
	}
	if(!this.topicStartIndex) {
		if(this.typeId == "Composition") {
			return errorMessage.CompositionStartIndexIsEmpty;
		}
		return errorMessage.StartIndexIsEmpty;
	}
	if(this.typeId != "Judge" && this.typeId != "AnswerQuestion") {
		if(!this.topicSelectCount) {
			if(this.typeId == "EnglishComposition") {
				return errorMessage.EnglishCompositionIsEmpty;
			} else if(this.typeId == "FillBlank") {
				return errorMessage.FillBlankSelectCountIsEmpty;
			}
			return errorMessage.SelectCountIsEmpty;
		}
	}
	if(!this.topicEndIndex) {
		if(this.typeId == "Composition") {
			return;
		}
		return errorMessage.TopicEndIndexIsEmpty;
	}
	//誊分模式限制
	if(!this.score && DescCtrl.getAnswerSheetModel()) {
		return errorMessage.ScoreIsEmpty;
	}
	return "";
};

/**
 * 判断文本框是否只是数字
 * 题目名称判断是否满足指定格式
 */
TopicCommonModule.prototype.checkTextIsDigit = function() {
	var reg = /^[0-9]*$/;
	var paperNameReg = /^[\s*a-zA-Z0-9、，,：:\-——（）()_\u4e00-\u9fa5]+$/;
	if(!paperNameReg.test(this.topicName)) {
		return errorMessage.topicNameIsError;
	}
	if(!reg.test(this.topicStartIndex)) {
		if(this.typeId == "Composition") {
			return errorMessage.CompositionStartIndexIsDigit;
		}
		return errorMessage.StartIndexIsDigit;
	}
	if(this.typeId != "Judge" && this.typeId != "AnswerQuestion") {
		if(!reg.test(this.topicSelectCount)) {
			if(this.typeId == "Composition") {
				return errorMessage.CompositionCountIsDigit;
			} else if(this.typeId == "EnglishComposition") {
				return errorMessage.EnglishCompositionCountIsDigit;
			} else if(this.typeId == "FillBlank") {
				return errorMessage.FillBlankSelectCountIsDigit;
			} else {
				return errorMessage.SelectCountIsDigit;
			}
		}
	}
	if(!reg.test(this.topicEndIndex)) {
		return errorMessage.TopicEndIndexIsDigit;
	}
	if(isNaN(this.score)) {
		return errorMessage.ScoreIsDigit;
	}
	return "";
};

/**
 * 判断文本框数据是否超过最大值
 */
TopicCommonModule.prototype.checkTextIsOverflow = function() {
	var chineseRegex = /[^\x00-\xff]/g;
	//var strLength = this.topicName.replace(chineseRegex, "**").length;
	var strLength = this.topicName.length;
	if(strLength > 33) {
		return errorMessage.TopicNameIsOverflow;
	}
	if(this.topicStartIndex == 0) {
		return errorMessage.StartIndexIsZero;
	}
	if(this.topicEndIndex == 0) {
		return errorMessage.TopicEndIndexIsZero;
	}
	if(this.typeId != "Judge" && this.typeId != "AnswerQuestion") {
		if(this.topicSelectCount == 0) {
			if(this.typeId == "FillBlank") {
				return errorMessage.FillBlankSubfieldCountIsZero;
			} else if(this.typeId == "Composition") {
				return errorMessage.CompositionIsZero;
			} else if(this.typeId == "EnglishComposition") {
				return errorMessage.EnglishCompositionCountIsDigit;
			} else {
				return errorMessage.SelectCountIsZero;
			}
		}
		if(this.typeId == "Composition") {
			if(parseInt(this.topicSelectCount) > parseInt(this.compositionMaxCount)) {
				return errorMessage.CompositionCountOverflow;
			}
		} else if(this.typeId == "FillBlank") {
			if(parseInt(this.topicSelectCount) > parseInt(this.subfieldMaxCount)) {
				return errorMessage.FillBlankSubfieldCountOverflow;
			}
		} else if(this.typeId == "EnglishComposition") {
			if(parseInt(this.topicSelectCount) > parseInt(this.EnglishCompositionMaxCount)) {
				return errorMessage.EnglishCompositionCountOverflow;
			}
		} else {
			if(parseInt(this.topicSelectCount) > parseInt(this.selectMaxCount)) {
				return errorMessage.SelectCountOverflow;
			}
			if(parseInt(this.topicSelectCount) < parseInt(this.selectMinCount)) {
				return errorMessage.SelectCountMinflow;
			}
		}
	}
	if(parseInt(this.topicStartIndex) > parseInt(this.topicEndIndex)) {
		return errorMessage.TopicStartIndexLtEndIndex;
	}

	if(this.score >= 100 || this.score < 0) {
		return errorMessage.ScoreGt100;
	} else if(this.score.toString().split(".").length > 1 && this.score.toString().split(".")[1].length > 2) {
		return "小数点位数超过两位！";
	}
	return "";
};

/**
 * 判断是否超过题目最大限度
 */
TopicCommonModule.prototype.checkLtMaxCount = function() {
	this.createTopicCount = parseInt(this.topicEndIndex) - parseInt(this.topicStartIndex) + 1;
	if(!this.isCreateTopicType) {
		var topicCount = DescCtrl.getTopicCountByTopicTypeDescId(this.topicTypeDescId);
		this.maxTopicIndex = parseInt(topicCount) + parseInt(this.createTopicCount);
	} else {
		this.maxTopicIndex = parseInt(this.createTopicCount);
	}
	if(this.typeId == "FillBlank") {

		if(this.colnum < 2 && this.maxTopicIndex > 24) {
			return errorMessage.FillBlankOverflow24;
		}
		if(this.createTopicCount > this.fillBlankMaxCount || this.maxTopicIndex > this.fillBlankMaxCount) {
			return errorMessage.FillBlankOverflow;
		}

		/*if (this.createTopicCount > this.topicMaxCount) {
		 return errorMessage.TopicCountOverflow;
		 }*/
	} else if(this.typeId != "Object") {
		if(this.createTopicCount > this.topicMaxCount || this.maxTopicIndex > this.topicMaxCount) {
			return errorMessage.TopicCountOverflow;
		}
	} else {
		//客观题
		if(this.createTopicCount > this.singleMaxCount || this.maxTopicIndex > this.singleMaxCount) {
			return errorMessage.TopicCountOverflow;
		}
	}
	return "";
};

/**
 * 判断是否添加重复题号
 */
TopicCommonModule.prototype.checkIsRepeat = function() {
	var result = DescCtrl.checkCreateTopicIsRepeat(this.topicTypeDescId, parseInt(this.topicStartIndex), parseInt(this.topicEndIndex));
	return result ? "" : errorMessage.TopicIsRepeat;
};

/**
 * 各题型弹出框显示
 * isTranscriptsPoints 是否誊分模式
 */
TopicCommonModule.prototype.popShow = function(isTranscriptsPoints) {
	this.setPopData2();
	var typeId = this.typeId;
	$("a[name='btn_topics']").removeAttr("class");
	$("#btn_" + typeId).attr("class", "btn_active");
	$("#pop_" + typeId).show();
	$("#showMask").show();
	//是否显示题目类型的行
	if(this.isCreateTopicType) {
		//添加新题型，隐层单选或双选
		$("#ChoiceTypeRow_" + this.typeId).hide();
	} else {
		$("#ChoiceTypeRow_" + this.typeId).show();
	}

	//每题 * 分
	var score = $("#txt_" + typeId + "_score").val();
	if(!score) {
		$("#txt_" + typeId + "_score").val(0);
	}

	$(".answerPanelRow").hide(); //显示题干：

	////填空题誊分模式为带框，非誊分模式不带框，新建自制不能选择是否带框
	//if (from == "manual") {
	//    $("tr[id=FillBlank_Type]").hide();
	//}
	if(isTranscriptsPoints) {
		$("#rdo_FillBlank_frame").prop("checked", true);
	} else {
		$("#rdo_FillBlank_prue").prop("checked", true);
	}
	document.onkeydown = function(event) {
		if(event) {
			if(event.keyCode == 13) {
				if($("#pop_" + typeId + ":visible").length > 0) {
					$("#btn_" + typeId + "_submit").trigger("click");
				}
			}
		}
	};
};

/**
 * 各题型弹出框隐藏
 */
TopicCommonModule.prototype.popHide = function() {
	$("a[name='btn_topics']").removeAttr("class");
	$("#txt_" + this.typeId + "_name").removeAttr("readonly");
	$("#txt_" + this.typeId + "_select_count").removeAttr("readonly");
	$("#pop_" + this.typeId).hide();

	$("#showMask").hide();
};

/**
 * 清除弹出框数据
 */
TopicCommonModule.prototype.clearPopData = function() {
	this.clearMsg();
	var name = this.getTopicName();
	$("#txt_" + this.typeId + "_select_count").attr("disabled", false);

	$("#pop_" + this.typeId).removeAttr("dataTag");

	$("#txt_" + this.typeId + "_name").val(name);
	$("#txt_" + this.typeId + "_start_index").val(1);
	$("#txt_" + this.typeId + "_end_index").val("");
	$("#txt_" + this.typeId + "_score").val("");

	$("input[id=txt_" + this.typeId + "_select_count]").attr("disabled", false);
	$("input[name=objectType_" + this.typeId + "]").attr("disabled", false);
	$("tr[id=ChoiceTypeRow_" + this.typeId + "]").show();

	if(this.typeId != "Judge" && this.typeId != "AnswerQuestion") {
		if(this.typeId == "FillBlank") {
			$("#txt_" + this.typeId + "_select_count").val(2);
		} else if(this.typeId == "Composition") {
			$("#txt_" + this.typeId + "_end_index").val(1);
			$("#txt_" + this.typeId + "_select_count").val(900);
			//$("#pop_Composition div.pop_top_box h2").text("添加作文格数");
			$("#h_Composition_topicId").val("");
			//$("#btn_Composition_submit").text("添加").attr("title", "添加");
		} else if(this.typeId == "EnglishComposition") {
			$("#h_EnglishComposition_topicId").val("");
			//$("#pop_EnglishComposition div.pop_top_box h2").text("添加英语作文栏数");
			$("#txt_" + this.typeId + "_select_count").val(10);
		} else {
			$("#txt_" + this.typeId + "_select_count").val(4);
		}
	}
};

/**
 * 重新设置题干控制选项
 * 如果有题目存在隐藏题干，则设置为否
 */
TopicCommonModule.prototype.resetShowQuestionDesc = function() {
	var topicTypeDescs = DescCtrl.getTopicTypeDescs();
	topicTypeDescs.forEach(function(topicType) {
		if(topicType.subjectiveCategory != "Stem") {
			if(!topicType.showQuestionStem) {
				$("a[name='descCtrl']").each(function() {
					var display = $(this).attr("type");
					if(eval(display) == false) {
						$("a[name='descCtrl']").removeAttr("class");
						$(this).attr("class", "ra_active");
					}
				});

			}
		}

	});
};
/**
 * 清空提示信息
 */
TopicCommonModule.prototype.clearMsg = function() {
	$("span.msg").html("");
};
/**
 * 设置弹出框数据，只会在某个题型中添加小题的时候使用
 */
TopicCommonModule.prototype.setPopData = function() {
	var name = decodeURIComponent(DescCtrl.getTopicTitleByTopicTypeDescId(this.topicTypeDescId));
	name = $.trim(name.replace(/&nbsp;/g, " "));
	var nextIndex = DescCtrl.getTopicNextStartIndex2(this.topicTypeDescId);
	var optionCount = DescCtrl.getTopicOptionCount(this.topicTypeDescId);

	//增加如果是单选题或者是多选题，都显示客观题，否则正常来
	if(this.typeId == "SingleChoice" || this.typeId == "MultiChoice" || this.typeId == "Object") {
		$("#txt_" + this.typeId + "_name").val("客观题").attr("readonly", "readonly");
		//显示radio按钮的行
		if(this.isCreateTopicType) {
			$("#ChoiceTypeRow_" + this.typeId).hide();
		} else {
			$("#ChoiceTypeRow_" + this.typeId).show();
			$('input[name=objectType_' + this.typeId + ']').get(0).checked = true;
		}
	} else {
		$("#txt_" + this.typeId + "_name").val(name ? name : this.getTopicName()).attr("readonly", "readonly");
	}

	$("#txt_" + this.typeId + "_start_index").val(nextIndex);
	$("#txt_" + this.typeId + "_end_index").val("");
	if(this.typeId != "Judge" && this.typeId != "AnswerQuestion") {
		if(this.typeId == "FillBlank") {
			$("#txt_" + this.typeId + "_select_count").val(optionCount);
		} else {
			$("#txt_" + this.typeId + "_select_count").val(optionCount);
		}
	}
};
/**
 * 新建题型时，设置弹出框开始题号为：
 * 最后一个大题的 最后一个小题的题号+1。
 * 如果有noList字段，就取noList最后一个题号+1，
 */
TopicCommonModule.prototype.setPopData2 = function() {
	var topicTypeDescs = DescCtrl.getTopicTypeDescs();
	//var lastTopicDesc = null;
	var maxNo = 0;

	for(var i = topicTypeDescs.length - 1; i >= 0; i--) {
		var topicTypeDesc = topicTypeDescs[i];
		for(var j = topicTypeDesc.topicDescs.length - 1; j >= 0; j--) {
			//lastTopicDesc = topicTypeDesc.topicDescs[j];
			var noList = topicTypeDesc.topicDescs[j].noList;
			var tmpMaxIndex = noList ? noList[noList.length - 1] : topicTypeDesc.topicDescs[j].no;

			if(tmpMaxIndex > maxNo) {
				maxNo = tmpMaxIndex;
			}
		}

		$("#txt_" + this.typeId + "_start_index").val(maxNo + 1);
	}
};

/**
 * 获取题目名称
 */
TopicCommonModule.prototype.getTopicName = function() {
	var TopicNameList = {
		"SingleChoice": "单选题",
		"MultiChoice": "多选题",
		"Judge": "判断题",
		"FillBlank": "填空题",
		"AnswerQuestion": "解答题",
		"Composition": "语文作文题",
		"EnglishComposition": "英语作文题",
		"Object": "客观题"
	};
	return TopicNameList[this.typeId];
};

/**
 * 根据题号，获取当前小题所在数组的索引位置
 */
TopicCommonModule.prototype.getIndexbyNo = function(arr, no) {
	var index = 0;
	$.each(arr, function(i, v) {
		if(v.no == no || v.noList[0] == no) {
			index = i;
			return false;
		}
	});
	return index;
};